home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 251_01 / advjunk.c < prev    next >
Text File  |  1987-10-27  |  2KB  |  111 lines

  1. #include <stdio.h>
  2.  
  3. long _seed = 1L;
  4.  
  5. int rand()
  6. {
  7.     _seed *= 397204094L;
  8.     return (_seed & 0x7FFF);
  9. }
  10.  
  11. srand(n)
  12.   long n;
  13. {
  14.    _seed = n;
  15. }
  16.  
  17. int getch()
  18. {
  19.     int ch;
  20.     if ((ch = bdos(1) & 0xFF) == '\r') { bdos(6,'\n'); ch = '\n'; }
  21.     return (ch);
  22. }
  23.  
  24. waitch()
  25. {
  26.     bdos(7);
  27. }
  28.  
  29. putch(ch,fp)
  30.   int ch; FILE *fp;
  31. {
  32.     aputc(ch,fp);
  33. }
  34.  
  35. int advsave(hdr,hlen,save,slen)
  36.   char *hdr; int hlen; char *save; int slen;
  37. {
  38.     char fname[50];
  39.     int fd;
  40.  
  41.     trm_str("File name? ");
  42.     trm_get(fname);
  43.  
  44.     /* add the extension */
  45.     strcat(fname,".sav");
  46.  
  47.     /* create the data file */
  48.     if ((fd = creat(fname,0666)) == -1)
  49.     return (0);
  50.  
  51.     /* write the header */
  52.     if (write(fd,hdr,hlen) != hlen) {
  53.     close(fd);
  54.     return (0);
  55.     }
  56.  
  57.     /* write the data */
  58.     if (write(fd,save,slen) != slen) {
  59.     close(fd);
  60.     return (0);
  61.     }
  62.  
  63.     /* close the file and return successfully */
  64.     close(fd);
  65.     return (1);
  66. }
  67.  
  68. int advrestore(hdr,hlen,save,slen)
  69.   char *hdr; int hlen; char *save; int slen;
  70. {
  71.     char fname[50],hbuf[50],*p;
  72.     int fd;
  73.  
  74.     if (hlen > 50)
  75.     error("save file header buffer too small");
  76.  
  77.     trm_str("File name? ");
  78.     trm_get(fname);
  79.  
  80.     /* add the extension */
  81.     strcat(fname,".sav");
  82.  
  83.     /* create the data file */
  84.     if ((fd = open(fname,0)) == -1)
  85.     return (0);
  86.  
  87.     /* read the header */
  88.     if (read(fd,hbuf,hlen) != hlen) {
  89.     close(fd);
  90.     return (0);
  91.     }
  92.  
  93.     /* compare the headers */
  94.     for (p = hbuf; hlen--; )
  95.     if (*hdr++ != *p++) {
  96.         trm_str("This save file does not match the adventure!\n");
  97.         return (0);
  98.     }
  99.  
  100.     /* read the data */
  101.     if (read(fd,save,slen) != slen) {
  102.     close(fd);
  103.     return (0);
  104.     }
  105.  
  106.     /* close the file and return successfully */
  107.     close(fd);
  108.     return (1);
  109. }
  110.  
  111.